
use "E:\ProjektDB\706172\Workdata\706172\Husholdningsprojekt\Mortgage Choice and Expenditure\Data\robust_XyearsIO.dta" , clear
set matsize 10000
destring familie_id, replace
sort familie_id aar
xtset familie_id aar

rename treat_aar treat_aar1 /*Treat_aar er det år, afdragsfriheden udløber*/
tab treat_aar1
bys familie_id: egen treat_aar = min(treat_aar1)
tab treat_aar
drop treat_aar1
drop if treat_aar == . 

*Normalizing treatment year to 0
cap drop t_count
g t_count = aar - treat_aar

*drop if self-employed
cap drop temp
g temp = selvst 
cap drop selvst_panel
bys familie_id: egen selvst_panel = max(temp)
tab selvst_panel
drop if selvst_panel == 1

*drop if not tax liable 
cap drop temp
g temp = ikkeskattepligtig 
cap drop ikkeskattepligtig_panel
bys familie_id: egen ikkeskattepligtig_panel = max(temp)
tab ikkeskattepligtig_panel
drop if ikkeskattepligtig_panel == 1

*drop if property buying in years {-1,0,1}
recode koebt . = 0
cap drop temp
g temp = ((koebt == 1 | solgt == 1 | ejerskift == 1) & inrange(t_count,-1,1))
tab temp
cap drop koebere_frasort
bys familie_id: egen koebere_frasort=max(temp)
tab koebere_frasort
drop if koebere_frasort==1
			
gen m_IOstart = month(ym_afstart)
gen ym_IOstart = ym_afstart
format ym_IOstart %tdCCYYNN 

tab m_IOstart treat_aar
g temp_IOstart = (inrange(m_IOstart,1,3))
recode m_IOstart . = 0
tab temp_IOstart
bys familie_id: egen IOstart_frasort=max(temp_IOstart)
bys familie_id: egen IOstart_panel=max(m_IOstart)

**********************************
** COVARIATES					**
**********************************
*Family structure
cap drop numkids
g numkids = 0 if  famantboernf == 0
	replace numkids = 1 if  famantboernf == 1
	replace numkids = 2 if  famantboernf == 2
	replace numkids = 3 if  famantboernf>=3 & famantboernf != .

cap drop kids
g kids = 0
	replace kids = 1 if famantboernf != 0
cap drop temp
g temp = numkids if t_count == -2
cap drop numkids_t2
bys familie_id: egen numkids_m1 = max(temp)

cap drop civilstatus
g civilstatus = .
	replace civilstatus=0 if civst_1=="U"
	replace civilstatus=1 if civst_1=="G"
	replace civilstatus=2 if civst_1=="F"
	replace civilstatus=3 if civst_1=="P"
	replace civilstatus=4 if civst_1=="E"
	replace civilstatus=5 if civst_1=="L"
	replace civilstatus=6 if civst_1=="O"
tab civilstatus

g married = 0
	replace married = 1 if civst_1 == "G"
cap drop temp
g temp = married if t_count == -2
cap drop married_t2
bys familie_id: egen married_t2 = max(temp)

cap drop dti
g dti = famqpassivn/famperindkialt_13

cap drop bidragsrate
by familie_id (aar), sort: gen bidragsrate = (fambidrag/(famrestobl+famrestobl[_n-1])/2)

g male = (koen_1==1)
tab male

cap drop liq_assets
g liq_assets = fambankakt + famoblakt + famkursakt

recode famboligvaerdi . = 0
cap drop assets
g assets = fambankakt + famoblakt + famkursakt + famboligvaerdi

recode famrestobl . = 0
cap drop liabilities
g liabilities = fambankgaeld + famrestobl

cap drop ydelse
g ydelse= famafdrag_for + famrente_bet_nu + fambidrag  


*************************
** 2. LOAN VARIABLES   **
*************************

rename nytlaan newloan
recode newloan .=0
rename nytlaan_var_uden_afdrag newloan_AR_IO  /*Adjustable rate Interst only*/
recode newloan_AR_IO .=0
rename nytlaan_fast_uden_afdrag newloan_FR_IO /*Fixed rate Interst only*/
recode newloan_FR_IO .=0
rename nytlaan_var_med_afdrag newloan_AR_M    /*Adjustable rate paying back mortage*/
recode newloan_AR_M .=0
rename nytlaan_fast_med_afdrag newloan_FR_M   /*Fixed rate paying back mortage*/
recode newloan_FR_M .=0

recode overlev_lastyear . = 0
g newloan_io 	= (newloan_AR_IO==1 | newloan_FR_IO==1)
g newloan_noio 	= (newloan_AR_M==1 | newloan_FR_M==1)

g noloan		= (newloan==0 & overlev_lastyear==0)
g both_new_old 	= (newloan==1 & overlev_lastyear==1)
g only_newloan	= (newloan==1 & overlev_lastyear==0)
g only_loanstays= (newloan==0 & overlev_lastyear==1)

*New loan last year
cap drop new_lastyear
g new_lastyear = 0
	replace new_lastyear = 1 if newloan[_n-1] == 1 & familie_id == familie_id[_n-1]
tab new_lastyear
*New loan next year
cap drop new_nextyear
g new_nextyear = 0
	replace new_nextyear = 1 if newloan[_n+1] == 1  & familie_id == familie_id[_n+1]
tab new_nextyear

****

egen lenghtio=mean(l_afd), by (familie_id)
replace lenght=int(lenght)

** OUTCOME VARIABLES - loan uptake is in treatment year and one year before and after;
*Loan stays after 9 years and no new loan
cap drop out_loan_stays
g out_loan_stays = 0
	replace out_loan_stays = 1 if overlev_lastyear == 1 & newloan == 0 ///
		    & new_lastyear == 0 & new_nextyear == 0 & treat_aar == aar /*out_loan_stays takes value 1 in year t+10 */
tab out_loan_stays if treat_aar == aar

*Uptake of new IO loan after 9 years
cap drop out_newloan_io
g out_newloan_io = 0		 
	replace out_newloan_io = 1 if (newloan_io == 1 | (newloan_io[_n+1] == 1 & familie_id == familie_id[_n+1]) ///
			| (newloan_io[_n-1] == 1 & familie_id == familie_id[_n-1])) & treat_aar == aar
tab out_newloan_io if treat_aar == aar

*Uptake of new loan (but no IO loan)
cap drop out_newloan_noio
g out_newloan_noio = 0		 
	replace out_newloan_noio = 1 if (newloan + new_lastyear + new_nextyear >= 1) ///
		    & out_newloan_io != 1  & treat_aar == aar
tab out_newloan_noio if treat_aar == aar

*Pays out loan after 9 years. Possible explanations: pay out loan, kontantlån, new loan earlier/later, ...
cap drop out_paysout
g out_paysout = 0
	replace out_paysout = 1 if (out_loan_stays + out_newloan_io + out_newloan_noio == 0) ///
		    & treat_aar == aar
tab out_paysout if treat_aar==aar

count if out_newloan_io + out_newloan_noio + out_loan_stays + out_paysout > 1

tabstat out_newloan_io out_newloan_noio out_loan_stays out_paysout ///
		if treat_aar==aar, by(treat_aar) stat(mean n) 

*New loan last year
cap drop out_new_lastyear
g out_new_lastyear = 0
	replace out_new_lastyear = 1 if newloan[_n-1] == 1 & familie_id == familie_id[_n-1] & treat_aar == aar
tab out_new_lastyear

*New loan next year
cap drop out_new_nextyear
g out_new_nextyear = 0
	replace out_new_nextyear = 1 if newloan[_n+1] == 1  & familie_id == familie_id[_n+1] & treat_aar == aar
tab out_new_nextyear

tab out_new_lastyear out_new_nextyear if treat_aar==aar
tab out_new_lastyear out_new_nextyear if treat_aar==aar & noloan==1


*Pays out loan
cap drop temp
g temp = out_paysout if aar != 2017
cap drop paysout_panel
bys familie_id: egen paysout_panel = max(temp)
cap drop out_sample
g out_sample = (familie_id != familie_id[_n+1] & aar != 2017)
tab out_sample if out_paysout == 1

bys out_paysout: sum famrestobl fambankgaeld famboligvaerdi if treat_aar==aar
sum famboligvaerdi if paysout_panel == 1 & t_count == -1
sum famboligvaerdi if paysout_panel == 1 & t_count == 0
sum famboligvaerdi if paysout_panel == 1 & t_count == 1
sum solgt koebt ejerskift if (t_count == -1 | t_count == 0 | t_count == 1) & paysout_panel == 1

**Changes in dependent variables 
by familie_id (aar), sort: gen difffinaktiver = (fam_finaktiver - fam_finaktiver[_n-1])
by familie_id (aar), sort: gen diffindstpi = (famindestpi - famindestpi[_n-1])
by familie_id (aar), sort: gen diffbankgaeld = (fambankgaeld - fambankgaeld[_n-1])
by familie_id (aar), sort: gen diffconsump1a = (famforbrug1a - famforbrug1a[_n-1])
by familie_id (aar), sort: gen diffconsump3 = (famforbrug3 - famforbrug3[_n-1])
by familie_id (aar), sort: gen diffrestobl = (famrestobl - famrestobl[_n-1])
by familie_id (aar), sort: gen diffrestknt = (famrestknt - famrestobl[_n-1])
by familie_id (aar), sort: gen difffamafdrag_for = (famafdrag_for - famafdrag_for[_n-1])
by familie_id (aar), sort: gen diffliq_assets = (liq_assets - liq_assets[_n-1])
by familie_id (aar), sort: gen diffydelse = (ydelse - ydelse[_n-1])

*Changes in alternative consumption measure
by familie_id (aar), sort: gen diffconsump3_ales = [(famforbrug3[_n+1] + famforbrug3[_n+2]) - (famforbrug3[_n-1] + famforbrug3[_n-2])]/2
by familie_id (aar), sort: gen diffconsump1a_ales = [(famforbrug1a[_n+1] + famforbrug1a[_n+2]) - (famforbrug1a[_n-1] + famforbrug1a[_n-2])]/2

*Normalizing changes by past income 
by familie_id (aar), sort: gen rdifffinaktiver = (difffinaktiver / famdispon_13[_n-1])
by familie_id (aar), sort: gen rdiffindstpi = (diffindstpi / famdispon_13[_n-1])
by familie_id (aar), sort: gen rdiffbankgaeld = (diffbankgaeld / famdispon_13[_n-1])
by familie_id (aar), sort: gen rdiffconsump1a = (diffconsump1a / famdispon_13[_n-1])
by familie_id (aar), sort: gen rdiffconsump3 = (diffconsump3 / famdispon_13[_n-1])
by familie_id (aar), sort: gen rdiffrestobl = (diffrestobl / famdispon_13[_n-1])
by familie_id (aar), sort: gen rdiffrestknt = (diffrestknt / famdispon_13[_n-1])
by familie_id (aar), sort: gen rfamafdrag_for = (famafdrag_for / famdispon_13[_n-1])
by familie_id (aar), sort: gen rfamrente_bet_nu = (famrente_bet_nu / famdispon_13[_n-1])
by familie_id (aar), sort: gen rdiffconsump1a_ales = (diffconsump1a_ales / famdispon_13[_n-1])
by familie_id (aar), sort: gen rdiffconsump3_ales = (diffconsump3_ales / famdispon_13[_n-1])
by familie_id (aar), sort: gen rdiffliq_assets = (diffliq_assets / famdispon_13[_n-1])
by familie_id (aar), sort: gen rdydelse = (diffydelse / famdispon_13[_n-1])

by familie_id (aar), sort: gen rpensionsbidrag = (fampensindbetal / famdispon_13[_n-1])
by familie_id (aar), sort: gen rfamboligvaerdi = (famboligvaerdi / famdispon_13[_n-1])
by familie_id (aar), sort: gen rfamforbrug1a = (famforbrug1a / famdispon_13[_n-1])
by familie_id (aar), sort: gen rfamforbrug3 = (famforbrug3 / famdispon_13[_n-1])
by familie_id (aar), sort: gen rfamrestobl = (famrestobl / famdispon_13[_n-1])
by familie_id (aar), sort: gen rfambankgaeld = (fambankgaeld / famdispon_13[_n-1])
by familie_id (aar), sort: gen rliq_assets = (liq_assets / famdispon_13[_n-1])
by familie_id (aar), sort: gen rfamrentudgpr = (famrentudgpr / famdispon_13[_n-1])


*Censoring variables
local varlist 	fam_finaktiver difffinaktiver famindestpi diffindstpi rdiffindstpi fambankgaeld famrestobl diffbankgaeld bidragsrate ///
				diffrestobl famforbrug1a diffconsump1a famforbrug3 diffconsump3 diffconsump1a_ales rdiffconsump1a_ales ///
				rdifffinaktiver rdiffliq_assets rdiffindstpi rdiffbankgaeld famafdrag_for difffamafdrag_for rdiffrestobl rdiffrestknt ///
				rdiffconsump1a rdiffconsump3 rdiffconsump1a_ales rdiffconsump3_ales famperindkialt_13 famdispon_13 famloenmv_13 rfamboligvaerdi dti ///
				fampensindbetal rpensionsbidrag famdispon_13 rfamafdrag_for rfamrente_bet_nu rfamboligvaerdi rfamrestobl rfamforbrug1a rfamforbrug3 rfambankgaeld rliq_assets ///
				rfamrentudgpr

foreach v in `varlist' {
cap drop `v'_cens
g `v'_cens=`v'
forval t=1998/2017 {
qui sum `v' if aar==`t',d
replace `v'_cens=r(p1) if `v'_cens<r(p1) & `v'_cens!=. & aar==`t'
replace `v'_cens=r(p99) if `v'_cens>r(p99) & `v'_cens!=. & aar==`t'
}
}



**********************************************************
** Empirical model, event study
**********************************************************
cd "E:\ProjektDB\706172\Workdata\706172\Husholdningsprojekt\Mortgage Choice and Expenditure\Output"	 
cap drop etime
g etime=t_count+100	
cap drop EXP
g EXP=0
replace EXP=1 if etime>=100


sort familie_id aar
local varlist "fambankakt fambankgaeld famoblakt famoblgaeld famkursakt famdispon_13 famboligvaerdi"
foreach v in `varlist' {
cap drop temp
g temp=`v' if etime==89
cap drop `v'_pre
bys familie_id: egen `v'_pre=max(temp)
cap drop l2_`v'
bys familie_id: g l2_`v'=l2.`v'
cap drop l1_`v'
bys familie_id: g l1_`v'=l1.`v'
}
tabstat *_pre, by(aar)
tabstat l2_*, by(aar)
tabstat l1_*, by(aar)	

cap drop kom_cluster
g 		kom_cluster=kom
replace kom_cluster=kom[_n+1] if kom_cluster==. & familie_id==familie_id[_n+1] & aar==aar[_n+1]-1
replace kom_cluster=kom[_n-1] if kom_cluster==. & familie_id==familie_id[_n-1] & aar==aar[_n-1]+1
tabstat kom kom_cluster alder_1, by(aar) stats(n)

****************************************************************************************
** sample selection	
drop if kom_cluster==.

cap drop temp
g temp=1 if inrange(etime,99,100)
cap drop ekeep
bys familie_id: egen ekeep=total(temp)
tab ekeep
keep if ekeep==2 /* Remove if not in dataset in year of expiry and the year before*/



******************************************************************************************
** We split them on whether they hold on to the old loan or take up a new IO/non-IO loan

cap drop temp
g 		temp=.
** No new loan: hold on to existing mortgage
replace temp=0 if newloan==0 		& inrange(etime,100,100) & overlev_lastyear==1	& koebt!=1 & solgt!=1

** New IO loan replaces old IO loan
replace temp=1 if newloan_io==1 	& inrange(etime,100,100) & overlev_lastyear==0  & koebt!=1 & solgt!=1

** New Non-IO loan replaces old IO loan
replace temp=2 if newloan_noio==1	& inrange(etime,100,100) & overlev_lastyear==0	& koebt!=1 & solgt!=1

** Old mortage is prepaid when IO period expires
replace temp=3 if newloan==0 		& inrange(etime,100,100) & overlev_lastyear==0  & koebt!=1 & solgt!=1

** New IO loan replaces old IO loan 1 year before expiry
cap drop temp2
g temp2=.
replace temp2=1 if newloan_io==1 & inrange(etime,99,99) & overlev_lastyear==0 & koebt!=1 & solgt!=1
cap drop temp3
bys familie_id: egen temp3=max(temp2)
replace temp=4 if temp3==1 

** New Non-IO loan replaces old IO loan 1 year before expiry
cap drop temp2
g temp2=.
replace temp2=1 if newloan_noio==1 & inrange(etime,99,99) & overlev_lastyear==0 & koebt!=1 & solgt!=1
cap drop temp3
bys familie_id: egen temp3=max(temp2)
replace temp=5 if temp3==1 

** Prepaid prior to expiry
cap drop temp2
g temp2=.
replace temp2=1 if newloan==0 & inrange(etime,99,99) & overlev_lastyear==0 & koebt!=1 & solgt!=1
replace temp2=1 if newloan==0 & inrange(etime,98,98) & overlev_lastyear==0 & koebt!=1 & solgt!=1
cap drop temp3
bys familie_id: egen temp3=max(temp2)
replace temp=3 if temp3==1 & newloan==0 & inrange(etime,100,100) & overlev_lastyear==0 & koebt!=1 & solgt!=1

** Taking up additional mortgage loan without closing old mortgage
cap drop temp2
g temp2=.
replace temp=6 if newloan==1 & inrange(etime,100,100) & overlev_lastyear==1 & koebt!=1 & solgt!=1

** Labels
tab temp
cap drop type		
bys familie_id: egen type=max(temp)

cap label drop type
label define type 	0	"No new loan" ///
					1 	"IO"	///
					2 	"Non-IO"		///
					3 	"Prepaid (also pre-expiry)" ///
					4	"IO t-1"  ///
					5	"Non-IO t-1" ///
					6	"Top-up"
					
label val type type
tab type
drop if type==.			/*Remove about 1.8% of sample that is unclassified*/

** Count number of IO mortgage originations across months
tab treat_aar 
tab type if etime==99
hist ym_laanoptag , freq saving(fig1, replace)
cap drop y_afstart
g y_afstart=year(ym_afstart)
hist ym_afstart if y_afstart>=2004, freq width(31) 
graph export "hist_IOstart.png", replace

** indicator for origination in 1,2,3 or 4th quarter 
cap drop temp
g 		temp=4 if month(ym_afstart)<=12 & ym_afstart!=.
replace temp=3 if month(ym_afstart)<=9 & ym_afstart!=.
replace temp=2 if month(ym_afstart)<=6 & ym_afstart!=.
replace temp=1 if month(ym_afstart)<=3 & ym_afstart!=.
cap drop quarter
bys familie_id: egen quarter=max(temp)
tab quarter

** extract equity indicator (increasing mortgage debt by more than DKK 20,000)
cap drop temp
g 		temp=.
replace temp=1 if newloan==1 		& inrange(etime,100,100)	& diffrestobl>20000
cap drop extract		
bys familie_id: egen extract=max(temp)
replace extract=0 if extract==.
tab extract

** low cash on hand indicator 1 year before expiry
sum liq_assets famkursakt famoblakt fambankakt  famperindkialt_13
cap drop temp
g 		temp=1 if liq_assets< (famperindkialt_13/12) & etime==99
replace temp=0 if liq_assets>= (famperindkialt_13/12) & etime==99
cap drop lowliq
bys familie_id: egen lowliq=max(temp)
tab lowliq

** illiquid assets to total assets, indicator 1 year before expiry
cap drop illiquidassets
g illiquidassets=(famboligvaerdi)/(famboligvaerdi+famkursakt+famoblakt+fambankakt) if etime==99
cap drop temp
xtile 		temp=illiquidassets , n(4)
cap drop illiquidassets_xtile
bys familie_id: egen illiquidassets_xtile=max(temp)
tabstat illiquidassets, by(illiquidassets_xtile)

** LTV above 80% 1 year before expiry
cap drop temp
g 		temp=0 if fammaxltv< 80 & etime==99 & fammaxltv!=.
replace temp=1 if fammaxltv>= 80 & etime==99 & fammaxltv!=.
cap drop ltv80
bys familie_id: egen ltv80=max(temp)
tab ltv80

** DTI above 400% 1 year before expiry
cap drop temp
g 		temp=0 if dti< 4 & etime==99 & dti!=.
replace temp=1 if dti>= 4 & etime==99 & dti!=.
cap drop dti4
bys familie_id: egen dti4=max(temp)
tab dti4

** Combined dti-ltv limits
cap drop ltv80_dti4
g ltv80_dti4=ltv80*dti4
tab ltv80_dti4



** high ltv indicator 1 year before expiry (computed for each type 0,1,2, seperately)
cap drop temp
g temp=.
forval j=0/2 {
sum fammaxltv if etime==99 & type==`j',d
scalar medianltv=r(p50)
di medianltv

replace	temp=1 if fammaxltv>=medianltv & etime==99 & type==`j'
replace temp=0 if fammaxltv<medianltv & etime==99 & type==`j'
}
cap drop highltv
bys familie_id: egen highltv=max(temp)
tab highltv

** age groups
sum alder_1 if type==0,d
cap drop temp
g temp=0
replace temp=1 if alder_1>50
cap drop agegroup
bys familie_id: egen agegroup=max(temp)

cap label drop agegroup
label define agegroup 	0	"Younger" ///
						1 	"Older"						
label val agegroup agegroup
tab agegroup




cap drop temp
g temp=rfamforbrug3_cens if etime==98
cap drop baseC
bys familie_id: egen baseC=max(temp)
cap drop indexC*
g indexC=rfamforbrug3_cens-baseC
local varlist "indexC"
foreach v in `varlist' {
cap drop `v'_cens
g `v'_cens=`v'
forval t=1998/2017 {
qui sum `v' if aar==`t',d
replace `v'_cens=r(p1) if `v'_cens<r(p1) & `v'_cens!=. & aar==`t'
replace `v'_cens=r(p99) if `v'_cens>r(p99) & `v'_cens!=. & aar==`t'
}
}	

*****graph frequency



*****catplot



*****GENERATE VARIABLES

 drop dti
 gen dti=(famoblgaeld)/famperindkialt_13
 
 gen highdti=0 if etime==99
 replace highdti=1 if dti>4 & etime==99
 
 
 

  gen si=(fambankakt+famkursakt+famoblakt)/(famperindkialt_13/12)
replace si=. if si<0|si>62
drop lowliq
gen lowliq=0 if si!=.&etime==99
replace lowliq=1 if si<2&si!=.&etime==99

egen meanlow=mean(lowliq), by(familie_id)
global split "& meanlow==1"




****** CLEAN DATA

drop if famdispon_13<0
drop if dti<0|dti>100 
replace fammax=. if fammax>200
****DESCRITPIVES

***graph of choice
gen type2=type
replace type2=1 if type==4
replace type2=2 if type==5
replace type2=2 if type==5
replace type2=3 if type==6

catplot type2 aar if etime==100, percent(aar) stack vertical asyvars
***descriptives

bysort type2: sum fammax dti famdispon_13 alder_1 si 

hist fammax if type2==0
hist fammax if type2==1
			
************************************************************************************************			
** 
** Set globals for regressions			
*****************************************************************************
global selection 	"if  rfamafdrag_for_cens!=. & rdiffconsump3_cens!=. & inrange(etime,96,103)"
global typechoice " & type==0"			
global controls		"i.alder_1 i.koen_1 i.aar  l2_* i.famfastrente"
global cluster		"cluster(familie_id)"

**changes: start year is -4, baseline year is -4, fixed effects for the househ
		
egen hh=group(familie_id)

		
gen keep=1 if type==0
replace keep=0 if type==1
gen keep2=1 if type==0
replace keep2=0 if type==2
		
		
gen expired=0
replace expired=1 if etime>=100

gen IO=1 if type==1
replace IO=0 if type==0

drop if type==3|type==6

drop if lenght==13

keep if lenght>=6&lenght<8

gen repayearly=1
keep if type==0
 save "E:\ProjektDB\706172\Workdata\706172\Husholdningsprojekt\Mortgage Choice and Expenditure\Data\controlgroup.dta", replace